package com.rk.kbk.controller;

import com.rk.kbk.common.JsonResp;
import com.rk.kbk.common.RandomUtils;
import com.rk.kbk.controller.wxutils.ConfKit;
import com.rk.kbk.controller.wxutils.MessageUtil;
import com.rk.kbk.controller.wxutils.WeixinApiTicket;
import com.rk.kbk.controller.wxutils.WeixinToken;
import com.rk.kbk.model.TextMessage;
import com.rk.kbk.service.UserService;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.util.Date;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.user;

/**
 * Created by bilibili on 2017/5/11.
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/wx")
public class wxController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private UserService userService;
    /**
     * 微信登录
     * @param phone 用户名
     * @param code 验证码
     * @return JsonResp
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/wxLogin",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp wxLogin(String phone, String code) throws UnsupportedEncodingException {
        log.debug("微信登录");
        return userService.wxLogin(URLDecoder.decode(phone,"utf-8"), code,1);

    }



    /**
     * @获取微信当前登陆者的位置信息
     *
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/getLoginUserPlace",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp getIndex(String urlParameter) throws Exception {
        String jsapi_ticket = WeixinApiTicket.getRecentTicket(WeixinToken.getRecentToken());
       // String urlAddress = urlParameter.substring(urlParameter.indexOf("//")+2);
        String urlAddress = urlParameter;
        Map<String, String> ret = new HashMap<String, String>();
        String nonce_str = RandomUtils.getRandomString(32);
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String string1;
        String signature = "";

        //注意这里参数名必须全部小写，且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
                "&noncestr=" + nonce_str +
                "&timestamp=" + timestamp +
                "&url=" + urlAddress;

        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        crypt.reset();
        crypt.update(string1.getBytes("UTF-8"));
        signature = byteToHex(crypt.digest());
        ret.put("url", urlAddress);
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);
        ret.put("appId", ConfKit.get("AppId"));
        return JsonResp.ok(ret);

    }
    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash)
        {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    /**
     * 处理微信发来的请求
     *
     * @param request
     * @return
     */
    public String processRequest(HttpServletRequest request, HttpServletResponse response){
        String respMessage = null;
        try {
            // 默认返回的文本消息内容
            String respContent = "请求处理异常，请稍候尝试！";
            // xml请求解析
            Map<String, String> requestMap = MessageUtil.parseXml(request);
            // 发送方帐号（open_id）
            String fromUserName = requestMap.get("FromUserName");
            request.setAttribute("openId", fromUserName);
            HttpSession session=request.getSession();
            session.setAttribute("openId", fromUserName);

            // 公众帐号
            String toUserName = requestMap.get("ToUserName");
            // 带参，二维码
            String eventKey = requestMap.get("EventKey");
            // 消息类型
            String msgType = requestMap.get("MsgType").toLowerCase();
            // 回复文本消息
            TextMessage textMessage = new TextMessage();
            textMessage.setToUserName(fromUserName);
            textMessage.setFromUserName(toUserName);
            textMessage.setCreateTime(new Date().getTime());
            textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
            textMessage.setFuncFlag(0);
            // 文本消息
            if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
                respContent = "您发送的是文本消息！";
            }
            // 图片消息
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
                respContent = "您发送的是图片消息！";
            }
            // 地理位置消息
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
                respContent = "您发送的是地理位置消息！";
            }
            // 链接消息
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
                respContent = "您发送的是链接消息！"+requestMap.get("EventKey");
            }
            // 音频消息
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
                respContent = "您发送的是音频消息！";
            }
            // 事件推送
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
                // 事件类型
                String eventType = requestMap.get("Event").toLowerCase();
                // 自动获取地理位置
                if (eventType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
             /*       Example example = userService.getExample();
                    example.createCriteria().andEqualTo("openid",fromUserName).andEqualTo("status",1);
                    User user = userService.selectFirst(example);*/

                    if(user != null){
                        //删除之前旧数据
              /*          Position position = new Position();
                        position.setUserId(user.getId());
                        positionService.delete(position);

                        position.setServerType(user.getServerType());
                        position.setLng(Float.valueOf(requestMap.get("Longitude")));
                        position.setLat(Float.valueOf(requestMap.get("Latitude")));
                        positionService.add(position);*/
                    }

                    respContent = "地理位置！";
                }
                // 订阅
                if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {

                    String access_token = WeixinToken.getRecentToken();

                    String urlStr = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+fromUserName;
                    URL url = new URL(urlStr);
                    BufferedReader bufr = new BufferedReader(new InputStreamReader(new BufferedInputStream(url.openStream()), "utf-8"));
                    String line;
                    StringBuffer sb=new StringBuffer();
                    while((line=bufr.readLine())!=null){
                        sb.append(line);
                    }
                    bufr.close();
                    JSONObject jsonObject = JSONObject.fromObject(sb.toString());
                    if(jsonObject.get("errcode") != null){
                        throw new Exception(jsonObject.getString("errmsg"));
                    }


                    String nick = String.valueOf(jsonObject.get("nickname"));
                   // nick = EmojiFilter.filterEmoji(nick);
                    String sex = String.valueOf(jsonObject.get("sex"));
                    if (sex.equals("1")) {
                        sex = "男";
                    } else if(sex.equals("2")) {
                        sex = "女";
                    } else if(sex.equals("0")) {
                        sex = "未知";
                    }
                    String city = String.valueOf(jsonObject.get("city"));
                    String province = String.valueOf(jsonObject.get("province"));
                    String country = String.valueOf(jsonObject.get("country"));
                    String headimgurl = String.valueOf(jsonObject.get("headimgurl"));

                    /*//用户信息更新或者添加
                    Example example = userService.getExample();
                    example.createCriteria().andEqualTo("openid",fromUserName);
                    User resultUser = userService.selectFirst(example);
                    if (resultUser == null){
                        resultUser = new User();
                    }

                    resultUser.setOpenid(fromUserName);
                    resultUser.setNickName(nick);
                    resultUser.setSex(sex);
                    resultUser.setCity(city);
                    resultUser.setProvince(province);
                    resultUser.setCountry(country);
                    resultUser.setHeadImgurl(headimgurl);
                    if(resultUser.getId() != null){
                        //被封号的不能关注
                        if(resultUser.getStatus().equals(3)){
                            log.debug("关注成功");
                            return "关注失败！";
                        }
                        resultUser.setStatus(1);
                        userService.updateByPrimaryKey(resultUser);
                    }else{
                        resultUser.setUserType(1);  //用户
                        resultUser.setStatus(1);
                        resultUser.setAdminId(1L); //默认总公司
                        userService.add(resultUser);

                        //若参数中包含qrscene_,则获取当前用户,设置分销
                        if(eventKey.indexOf("qrscene_") != -1){
                            resultUser = userService.selectFirst(example);
                            userService.setTeamUser(resultUser.getId(),Long.parseLong(eventKey.split("qrscene_")[1]));
                        }

                        User reurnUser = userService.selectFirst(example);
                        //创建用户账户信息
                        UserAccount userAccount = new UserAccount();
                        userAccount.setUserId(reurnUser.getId());
                        userAccount.setAllMoney(new BigDecimal(0));
                        userAccount.setBlockMoney(new BigDecimal(0));
                        userAccount.setAllCredit(0);
                        userAccount.setBlockCredit(0);
                        userAccount.setAccountLevel(1);
                        userAccount.setAccountStatus(1);
                        userAccountService.add(userAccount);
                    }
                    respContent = "谢谢您的关注！";
                    log.debug("关注成功");*/

                }
                // 取消订阅
                else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
                /*    Example example = userService.getExample();
                    example.createCriteria().andEqualTo("openid",fromUserName);
                    User resultUser = userService.selectFirst(example);
                    resultUser.setStatus(2);
                    userService.updateByPrimaryKeySelectiveJson(resultUser);*/
                    // TODO 取消订阅后用户再收不到公众号发送的消息，因此不需要回复消息
                }
                // 自定义菜单点击事件
                else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
                    // TODO 自定义菜单权没有开放，暂不处理该类消息
                    String key = requestMap.get("EventKey");
                    if(key.equals("MAINTAIN")){
                        respContent = "";
                    }
                    if(key.equals("INFO")){
                        respContent = "";
                    }
                    System.out.println("event key:"+key);
                }
                // 自定义view界面
                else if (eventType.equals(MessageUtil.EVENT_TYPE_VIEW)){
                    String url = requestMap.get("EventKey");
                    System.out.println("url:"+url);

                    respMessage = MessageUtil.textMessageToXml(textMessage);
                }
            }
            return respContent;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return respMessage;

    }




}
